package example.tallydemo.MyActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import example.tallydemo.MyView.SettingDialog;
import example.tallydemo.R;

public class SettingActivity extends AppCompatActivity
{

    String money;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        //设置首选项
        loadConfig();
    }

    public void onsetMoney(View view)
    {
        SettingDialog slg = new SettingDialog();

        slg.linstener = new SettingDialog.OnDiaLogListener()
        {
            @Override
            public void onDiaLogSetMoney(String money)
            {
                ((TextView)findViewById(R.id.id_setting_money)).setText(money);
            }
        };
        slg.show(getFragmentManager(),"SettingDialog");



    }

    // 加载配置
    public void loadConfig()
    {
        SharedPreferences sharedPref = getSharedPreferences("set", Context.MODE_PRIVATE); //可以在多个Acitivy里访问的首选项
        money = sharedPref.getString("money","20");
        ((TextView)findViewById(R.id.id_setting_money)).setText(money);
    }

    public void setConfig()
    {
        SharedPreferences sharedPref = getSharedPreferences("set", Context.MODE_PRIVATE); //可以在多个Acitivy里访问的首选项
        money = ((TextView)findViewById(R.id.id_setting_money)).getText().toString();
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("money", money);
        editor.commit();
        Toast.makeText(SettingActivity.this, "设置成功 重启生效", Toast.LENGTH_SHORT).show();
    }

    public void onSetOk(View view)
    {
        setConfig();
        onBack(view);
    }

    public void onBack(View view)
    {
        super.onBackPressed();
    }
}
